﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using System.Text;

using System.Net.Http;
using System.Web.Http;
using System.Data;
using System.Xml;
using System.Net.Http.Headers;
using System.Text.RegularExpressions;

namespace MVC.Controllers
{
    public class CalendarController : Controller
    {
        // GET: Calendar
        public ActionResult Index()
        {
            return View();
        }

        // GET: ZimbraCalendar
        public void ZimbraCalendar(string command, bool Params = false)
        {
            string CalendarURL = "";

            if (command.Contains("ICS"))
            {
                CalendarURL = "https://mail.rva.org/home/calendar@rva.org/" + command.Remove(command.IndexOf("ICS")) + ".ics";
            }
            else
            {
                CalendarURL = "https://mail.rva.org/home/calendar@rva.org/" + command + ".html";
            }

            if (Params)
            {
                CalendarURL += "?" + Request.QueryString;
            }

            string results = WebFuncs.LoadTextFile(CalendarURL,
                                System.Configuration.ConfigurationManager.AppSettings["CalendarLogin"],
                                System.Configuration.ConfigurationManager.AppSettings["CalendarPassword"]);
            if (!command.Contains("ICS"))
            {
                // get rid of the "favicon"
                results = results.Replace("<link rel=\"SHORTCUT ICON\" href=\"/img/logo/favicon.ico\">", "");
                // remove the rss feed
                results = results.Replace("<link rel=\"alternate\" type=\"application/rss+xml\"  title=\"RSS Feed\" href=\"Testboard.rss\">", "");
                // fix links
                results = results.Replace("?", "?command=" + command + "&Params=true&");
                //var regmine = "(" + Regex.Escape("/Calendar/ZimbraCalendar?command=" + command + "&Params=true&") + ".*\\\")";
                //results = Regex.Replace(results, "(" + Regex.Escape("/Calendar/ZimbraCalendar?command=" + command + "&Params=true&") + ".*)\\\">", "a$1a\\\">");
                // fix image map
                results = results.Replace("/img/", "/Calendar/ZMpng?value=");
                // replace the css url with a local url that will load the css data and display it
                results = results.Replace("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/common,login,images,skin.css",
                    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Calendar/ZMcss?value=common\" />" +
                    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Calendar/ZMcss?value=login\" />" +
                    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Calendar/ZMcss?value=images\" />" +
                    "<link rel=\"stylesheet\" type=\"text/css\" href=\"/Calendar/ZMcss?value=skin.css\" />" + "<"
                    );
                results = results.Replace("/home/calendar@rva.org/" + command + ".ics", "/Calendar/ZimbraCalendar?command=" + command + "ICS");
            }
            else
            {
                Response.ContentType = "text/calendar; charset=UTF-8";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + command.Remove(command.IndexOf("ICS")) + ".ics");
            }



            // Write the contents of results directly to the screen.
            var encoding = new System.Text.UTF8Encoding();
            byte[] data = encoding.GetBytes(results);
            Response.OutputStream.Write(data, 0, data.Length);
            Response.OutputStream.Flush();
        }

        // GET: ZMcss files
        public void ZMcss(string value)
        {
            string CssURL = "";

            switch (value)
            {
                case "common":
                    CssURL = "https://mail.rva.org/css/common.css";
                    break;
                case "login":
                    CssURL = "https://mail.rva.org/css/login.css";
                    break;
                case "images":
                    CssURL = "https://mail.rva.org/css/images.css";
                    break;
                case "skin.css":
                    CssURL = "https://mail.rva.org/css/skin.css?client=standard&skin=steel&v=141215154045";
                    break;
                default:
                    CssURL = "";
                    break;
            }
            string results = WebFuncs.LoadTextFile(CssURL,
                    System.Configuration.ConfigurationManager.AppSettings["CalendarLogin"],
                    System.Configuration.ConfigurationManager.AppSettings["CalendarPassword"]);
            results = results.Replace("?", "___");
            results = results.Replace("/img/", "/Calendar/ZMpng?value=");
            results = results.Replace("___", "&");

            // Write the contents of results directly to the screen.
            var encoding = new System.Text.UTF8Encoding();
            byte[] data = encoding.GetBytes(results);
            Response.ContentType = "text/css";
            Response.OutputStream.Write(data, 0, data.Length);
            Response.OutputStream.Flush();
        }

        // GET: ZMpng files
        public void ZMpng(string value)
        {
            string pngURL = "https://mail.rva.org/img/" + value;

            Byte[] results = WebFuncs.LoadBinaryFile(pngURL,
                    System.Configuration.ConfigurationManager.AppSettings["CalendarLogin"],
                    System.Configuration.ConfigurationManager.AppSettings["CalendarPassword"]);
            // Write the contents of results directly to the screen.
            var encoding = new System.Text.UTF8Encoding();
            Response.ContentType = "image/png";
            Response.OutputStream.Write(results, 0, results.Length);
            Response.OutputStream.Flush();
        }

        public ActionResult ZMCalendarView(string calname)
        {
            ViewBag.URL = "/calendar/ZimbraCalendar?command=" + calname;
            return PartialView();
        }
    }
}